import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/aha_jetour_line.dart';
import 'package:jetour_line/model/city_list_model.dart';
import 'package:jetour_line/models/public_msg_manger.dart';
import 'package:jetour_line/models/user_info_model.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/pages/common/commonDetail/common_detail_page.dart';
import 'package:jetour_line/pages/home/city_choose_page.dart';
import 'package:jetour_line/pages/line/lineDetail/line_detail_page.dart';
import 'package:jetour_line/pages/webview/webview_page.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/login_utils.dart';

import '../../base_widget/banner_widget.dart';
import '../../base_widget/base_image.dart';
import '../../base_widget/image_network_widget.dart';
import '../../base_widget/sliver_list_widget.dart';
import '../../models/swiper_model.dart';
import '../../network/jetour_http.dart';
import '../../utils/page_push_utils.dart';
import '../group/join_group_page.dart';
import '../mine/mine_page.dart';

typedef _ChangeCityCallBack = void Function(CityModel cityModel);

class HomeHeadWidget extends StatefulWidget {
  final List<IndexSwiperModel> swiperData;
  final _ChangeCityCallBack callBack;
  String cityName;

  HomeHeadWidget({
    Key? key,
    required this.cityName,
    required this.swiperData,
    required this.callBack,
  }) : super(key: key);

  @override
  State<HomeHeadWidget> createState() => _HomeHeadWidgetState();
}

class _HomeHeadWidgetState extends State<HomeHeadWidget> {
  String? headUrl;
  List<String> images = [];
  UserInfoModel? userInfoModel;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.swiperData.isNotEmpty && images.isEmpty) {
      for (int i = 0; i < widget.swiperData.length; i++) {
        images.add(widget.swiperData[i].image);
      }
    }
    userInfoModel = PublicMsgManger().userInfoModel;
    if (userInfoModel != null) {
      headUrl = userInfoModel!.avatar;
    } else {
      headUrl = null;
    }
    return SliverListWidget.getSliverList(1, (context, index) {
      return Container(
        padding: EdgeInsets.only(left: 20.px, right: 20.px),
        width: double.infinity,
        child: Column(
          children: [
            Row(
              children: [
                GestureDetector(
                  onTap: () {
                    PagePushUtils.push(context, ChooseCityScreen())
                        .then((value) {
                      CityModel model = value as CityModel;
                      widget.callBack(model);
                      setState(() {
                        widget.cityName = model.name;
                      });
                    });
                  },
                  child: Container(
                    color: Colors.transparent,
                    child: Row(
                      children: [
                        BaseImage(
                          image: 'icon_addressblack',
                          width: 48.px,
                          height: 48.px,
                        ),
                        Text(
                          widget.cityName.isEmpty?"定位":widget.cityName,
                          style: TextStyle(
                              fontSize: 30.px, color: MyColors.color333333),
                        ),
                        BaseImage(
                          image: 'btn_open',
                          width: 40.px,
                          height: 40.px,
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                    child: SizedBox(
                  width: double.infinity,
                  height: 104.px,
                )),
                GestureDetector(
                  onTap: () {
                    AhaManger manger = AhaManger();
                    if (PublicMsgManger().userInfoModel == null) {
                      if (manger.loginCallBack != null) {
                        manger.loginCallBack!((String token) {
                          if (token.isNotEmpty) {
                            LoginUtils.login(token, (bool isLogin) {
                              if (isLogin) {
                                setState(() {
                                  headUrl =
                                      PublicMsgManger().userInfoModel!.avatar;
                                });
                              }
                            });
                          }
                        });
                      }
                    } else {
                      PagePushUtils.push(context, const MineScreen());
                    }
                  },
                  child: Container(
                    color: Colors.transparent,
                    child: Row(
                      children: [
                        // headUrl == null
                        //     ?
                        ClipRRect(
                          borderRadius: BorderRadius.all(Radius.circular(30.px)),
                          child: BaseImage(
                            image: 'icon_i',
                            width: 60.px,
                            height: 60.px,
                          ),
                        ),
                        // : ImageNetworkWidget(
                        //     headUrl!,
                        //     width: 60.px,
                        //     height: 60.px,
                        //     radius: 30.px,
                        //   ),
                        Text(
                          '个人中心',
                          style: TextStyle(
                              fontSize: 28.px, color: MyColors.color666666),
                        ),
                      ],
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    PagePushUtils.push(context, const JoinGroupScreen());
                  },
                  child: Container(
                    margin: EdgeInsets.only(right: 10.px),
                    color: Colors.transparent,
                    child: Row(
                      children: [
                        SizedBox(
                          width: 16.px,
                        ),
                        ClipRRect(
                          borderRadius: BorderRadius.all(Radius.circular(30.px)),
                          child: BaseImage(
                            image: 'icon_jrzd',
                            width: 60.px,
                            height: 60.px,
                          ),
                        ),
                        Text(
                          '加入组队',
                          style: TextStyle(
                              fontSize: 28.px, color: MyColors.color666666),
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
            images.isEmpty
                ? const SizedBox()
                : BannerWidget(
                    images,
                    width: double.infinity,
                    height: 240.px,
                    radius: 14.px,
                    selIndexCallBack: (index) {
                      swiperSel(index);
                    },
                  ),
            SizedBox(
              height: 24.px,
            )
          ],
        ),
      );
    });
  }

  void swiperSel(int index) {
    IndexSwiperModel model = widget.swiperData[index];
    if(model.jump.type.startsWith('app://')){//跳转捷途
      AhaManger ahaManger = AhaManger();
      if (ahaManger.shareCallBack != null) {
        String path = model.jump.type;
        if(model.jump.query.isNotEmpty){
          model.jump.query.forEach((key, value) {
            if(path ==  model.jump.type){
              path = "$path?$key=$value";
            }else{
              path = "$path&$key=$value";
            }
          });
        }
        print('goJetourCallBackpath = $path');//

        ahaManger.goJetourCallBack!(path);
      }
    }else if(model.jump.type.startsWith('jetour://')){//内部页面
      if(model.jump.type.contains('jetour://lineDetail.jump') ){//路书详情 jetour://lineDetail.jump
        if(model.jump.query.isNotEmpty){
          int id = int.parse(model.jump.query['id']);
          if(id>0){
            PagePushUtils.push(context, LineDetailPageScreen(id: id));
          }
        }
      }else  if(model.jump.type.contains('jetour://guideDetail.jump') ){//共创详情
        if(model.jump.query.isNotEmpty){
          int id = int.parse(model.jump.query['id']);
          if(id>0){
            PagePushUtils.push(context, CommonDetailScreen(id: id));
          }
        }
      }else  if(model.jump.type.contains('jetour://h5.jump') ){//webview
        if(model.jump.query.isNotEmpty){
          String url = model.jump.query['url'];
          if(url.isNotEmpty){
            PagePushUtils.push(context, WebviewScreen(url: url));
          }
        }
      }
    }

  }
}
